﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using Entity;
using BAL;
using Common;
using System.Text;

namespace Web
{
    public partial class AdminQuestion : System.Web.UI.Page
    {
        private IList<QuesTypeInfo> QuesTypeInfos = new List<QuesTypeInfo>();
        private IList<SubjectInfo> SubjectInfos = new List<SubjectInfo>();
        private IList<StatusInfo> stateinfos = new List<StatusInfo>();
        private QuestionInfo Question_Info = new QuestionInfo();
        private static UserLinkExtendInfo userinfo = new UserLinkExtendInfo();
        public static Module_Page Pages = new Module_Page();
        private static int _QuesTypeId = 0, _SubjectId = 0, _state = 0;
        private static string _Keyword = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!UserValidate.TeacherCheckLogin(this.Request))
            {
                Response.Write("<script type=\"text/javascript\">top.location.href='login.aspx';</script>");
                Response.Redirect("login.aspx");
                Response.End();
            }

            if (Request.QueryString["page"] != null && Request.QueryString["page"] != string.Empty)
            {
                if (PageValidate.IsNumber(Request.QueryString["page"]))
                {
                    Pages.PageNum = int.Parse(Request.QueryString["page"]);
                }
            }

            if (Request.QueryString["QuesTypeId"] != null && Request.QueryString["QuesTypeId"] != string.Empty)
            {
                if (PageValidate.IsNumber(Request.QueryString["QuesTypeId"]))
                {
                    _QuesTypeId = int.Parse(Request.QueryString["QuesTypeId"]);
                }
            }

            if (Request.QueryString["SubjectId"] != null && Request.QueryString["SubjectId"] != string.Empty)
            {
                if (PageValidate.IsNumber(Request.QueryString["SubjectId"]))
                {
                    _SubjectId = int.Parse(Request.QueryString["SubjectId"]);
                }
            }

            if (Request.QueryString["Keyword"] != null && Request.QueryString["Keyword"] != string.Empty)
            {
                _Keyword = Request.QueryString["Keyword"];
            }

            if (Request.QueryString["CognitiveLevelId"] != null && Request.QueryString["CognitiveLevelId"] != string.Empty)
            {
                if (PageValidate.IsNumber(Request.QueryString["CognitiveLevelId"]))
                {
                    Question_Info.CognitiveLevelId = int.Parse(Request.QueryString["CognitiveLevelId"]);
                }
            }

            if (Request.QueryString["DifficId"] != null && Request.QueryString["DifficId"] != string.Empty)
            {
                if (PageValidate.IsNumber(Request.QueryString["DifficId"]))
                {
                    Question_Info.DifficId = int.Parse(Request.QueryString["DifficId"]);
                }
            }

            if (Request.QueryString["KnowId"] != null && Request.QueryString["KnowId"] != string.Empty)
            {
                if (PageValidate.IsNumber(Request.QueryString["KnowId"]))
                {
                    Question_Info.KnowId = int.Parse(Request.QueryString["KnowId"]);
                }
            }

            if (Request.QueryString["ComputeTime"] != null && Request.QueryString["ComputeTime"] != string.Empty)
            {
                if (PageValidate.IsNumber(Request.QueryString["ComputeTime"]))
                {
                    Question_Info.ComputeTime = int.Parse(Request.QueryString["ComputeTime"]);
                }
            }

            if (Request.QueryString["BookId"] != null && Request.QueryString["BookId"] != string.Empty)
            {
                if (PageValidate.IsNumber(Request.QueryString["BookId"]))
                {
                    Question_Info.BookId = int.Parse(Request.QueryString["BookId"]);
                }
            }

            if (Request.QueryString["Score"] != null && Request.QueryString["Score"] != string.Empty)
            {
                if (PageValidate.IsNumber(Request.QueryString["Score"]))
                {
                    Question_Info.Score = decimal.Parse(Request.QueryString["Score"]);
                }
            }

            if (!IsPostBack)
            {
                userinfo = UserValidate.TeacherGetInfo(this.Request);
                LoadQuesType();
                LoadSubject();
                LoadCognitiveLevel();
                LoadDiffic();
                Question_Info.Score = 0;
                Question_Info.StatusId = 0;
                Question_Info.BookId = 0;
                Question_Info.CognitiveLevelId = 0;
                Question_Info.ComputeTime = 0;
                Question_Info.DifficId = 0;
                Question_Info.KeyWord = null;
                Question_Info.Printer = null;
                Question_Info.QuesTypeId = 0;
                Question_Info.SubjectId = 0;
                Question_Info.KnowId = 0;
                Question_Info.UserId = userinfo.Id;
                LoadQuestion();
            }
        }

        protected void LoadDiffic()
        {
            IList<DifficInfo> Difficinfos = Diffic.GetDifficInfos(0, 0);
            DifficIdList.Items.Clear();
            DifficIdList.Items.Add(new ListItem("请选择难度级别", "0"));
            foreach (DifficInfo info in Difficinfos)
            {  
                DifficIdList.Items.Add(new ListItem(info.Name+"    "+info.Keyword.ToString(), info.Keyword.ToString()));
            }
        }

        protected void LoadQuesType()
        {
            QuesTypeInfos = QuesType.GetQuesTypeInfos(0, 0);
            QuesTypeList.Items.Clear();
            QuesTypeList.Items.Add(new ListItem("试题类型", "0"));
            foreach (QuesTypeInfo info in QuesTypeInfos)
            {
                QuesTypeList.Items.Add(new ListItem(info.Name+"    "+info.Keyword.ToString(), info.Keyword.ToString()));
            }
        }

        protected void LoadSubject()
        {
            SubjectInfos = Subject.GetSubjectInfos();
            SubjectList.Items.Clear();
            SubjectList.Items.Add(new ListItem("课程", "0"));
            foreach (SubjectInfo info in SubjectInfos)
            {
                SubjectList.Items.Add(new ListItem(info.Name, info.Id.ToString()));//+"    "+info.Keyword.ToString()
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Question_Info.QuesTypeId = int.Parse(QuesTypeList.SelectedValue);
            Question_Info.SubjectId = int.Parse(SubjectList.SelectedValue);
            Question_Info.KeyWord = KeyWord.Text.Trim();
            Question_Info.Printer = null;
            Question_Info.StatusId = 0;
            Question_Info.DifficId = int.Parse(DifficIdList.SelectedValue);
            Question_Info.CognitiveLevelId = int.Parse(CognitiveLevelIdList.SelectedValue);
            Question_Info.UserId = userinfo.Id;
            if (BookList.SelectedValue.Trim() != "")
            {
                Question_Info.BookId = int.Parse(BookList.SelectedValue);
            }
            else
            {
                Question_Info.BookId = 0;
            }
            if (KnowIdList.SelectedValue.Trim() != "")
            {
                Question_Info.KnowId = int.Parse(KnowIdList.SelectedValue);
            }
            else
            {
                Question_Info.KnowId = 0;
            }

            //if (PageValidate.IsNumber(Score.Text.Trim()))
            //{
            //    Utility.ShowBoxyAlert("查询的分数必须是数字", this);
            //    return;
            //}
            //else if(Convert.ToDouble(Score.Text.Trim())<0 || Convert.ToDouble(Score.Text.Trim())>60)
            //{
            //    Utility.ShowBoxyAlert("查询的分数必须在（0~60）范围内", this);
            //    return;
            //}
            //else
            if (Score.Text.Trim()!=null && Score.Text.Trim()!="")
            {
                try
                {
                    
                    Double aa = Convert.ToDouble(Score.Text.Trim());
                    if (aa <= 0 || aa >60)
                    {
                        Utility.ShowBoxyAlert("查询的分数必须在（0.5~60）范围内", this);
                        return;
                    }
                }
                catch (Exception ex)
                {
                    Utility.ShowBoxyAlert("查询的分数必须是数字", this);
                    return;
                }
            }
            if (PageValidate.IsDecimal(Score.Text.Trim()))
            {
                Question_Info.Score = decimal.Parse(Score.Text.Trim());
            }
            else
            {
                Question_Info.Score = 0;
            }

            if (ComputeTime.Text.Trim()!=null && ComputeTime.Text.Trim()!="")
            {
                try
                {
                    Convert.ToDouble(ComputeTime.Text.Trim());
                }
                catch (Exception ex )
                {
                    Utility.ShowBoxyAlert("查询的时间必须是数字", this);
                    return;
                }
            
                if (Convert.ToDouble(ComputeTime.Text.Trim()) <= 0 || Convert.ToDouble(ComputeTime.Text.Trim()) > 5400)
                {
                    Utility.ShowBoxyAlert("查询的时间必须在（1~5400）范围内", this);
                    return;
                }
                else if (PageValidate.IsNumber(ComputeTime.Text.Trim()))
                {
                    Question_Info.ComputeTime = int.Parse(ComputeTime.Text.Trim());
                }
                else
                {
                    Question_Info.ComputeTime = 0;
                }
            }
            int quesTypeid = 0;
            if (QuesTypeList.SelectedValue != "0")
            {
                quesTypeid = QuesType.GetQuesTypeIdByKeyword(QuesTypeList.SelectedValue);
            }
            Question_Info.QuesTypeId = quesTypeid;
            int difficid = 0;//难度级别
            if (DifficIdList.SelectedValue != "0")
            {
                difficid = Diffic.GetDifficIDByKeyword(DifficIdList.SelectedValue);//通过关键字查询难度级别的编号
            }

            Question_Info.DifficId = difficid;
            int cognitiveLevelid = 0;//认知层次
            if (CognitiveLevelIdList.SelectedValue != "0")
            {
                cognitiveLevelid = CognitiveLevel.GetCognitiveLevelIDByKeyword(CognitiveLevelIdList.SelectedValue);//通过关键字查询认知层次的id ;
            }
            Question_Info.CognitiveLevelId = cognitiveLevelid;
            if (KnowIdList.SelectedValue.Trim() != "")
            {
                int knowId = 0;//知识点
                if (KnowIdList.SelectedValue != "0")
                {
                    knowId = Knowledge.GetKnowledgeIDByKeyword(KnowIdList.SelectedValue, Convert.ToInt32(SubjectList.SelectedValue));//通过关键字查询知识点的id
                }
                Question_Info.KnowId = knowId;
            }
            else
            {
                Question_Info.KnowId = 0;
            }
            LoadQuestion();
        }

        protected void LoadBook(int inputSubjectId)
        {
            IList<BookInfo> BookInfos = Book.GetBookInfosBySubject(inputSubjectId, 0, 0);
            BookList.Items.Clear();
            BookList.Items.Add(new ListItem("请选择适用范围", "0"));
            foreach (BookInfo info in BookInfos)
            {
                BookList.Items.Add(new ListItem(info.Name.ToString(), info.Id.ToString()));//+"    "+info.Keyword.ToString()
            }
        }

        protected void LoadCognitiveLevel()
        {
            IList<CognitiveLevelInfo> CognitiveLevelInfos = CognitiveLevel.GetList(0);
            CognitiveLevelIdList.Items.Clear();
            CognitiveLevelIdList.Items.Add(new ListItem("请选择认知层次", "0"));
            foreach (CognitiveLevelInfo info in CognitiveLevelInfos)
            {
                CognitiveLevelIdList.Items.Add(new ListItem(info.Name+"    "+info.Keyword.ToString(), info.Keyword.ToString()));
            }
        }

        protected void LoadKnowledge(int ParentID, int inputSubjectId, string line)
        {
            if (ParentID == 0)
            {
                KnowIdList.Items.Clear();
                KnowIdList.Items.Add(new ListItem("请选择知识点", "0"));
                IList<KnowledgeInfo> knowledgeInfos = Knowledge.GetRootKnowledgeInfo(0, inputSubjectId);
                foreach (KnowledgeInfo info in knowledgeInfos)
                {
                    ListItem item = new ListItem(info.Keyword+"  "+info.Name, info.Keyword.ToString());//+"    "+info.Keyword.ToString()
                    KnowIdList.Items.Add(item);
                    LoadKnowledge(info.Id, inputSubjectId, "|——");
                }
            }
            else
            {
                IList<KnowledgeInfo> knowledgeInfos = Knowledge.GetKnowledgeInfos(ParentID);
                foreach (KnowledgeInfo info in knowledgeInfos)
                {
                    ListItem item = new ListItem(line +info.Keyword+"  "+ info.Name, info.Keyword.ToString());//+"    "+info.Keyword.ToString()
                    KnowIdList.Items.Add(item);
                    LoadKnowledge(info.Id, inputSubjectId, "|——" + line);
                }
            }
        }

        protected void LoadQuestion()
        {
            Question_Info.UserId = userinfo.Id;
            Pages.AllCount = Question.GetQuestionCount(Question_Info);
            Pages.PageSize = 10;
            Pages.PageName = GetPageFileName(0);
            Pages.Compute();
            PageLabel.Text = Pages.GetPage();
            QList.DataSource = Question.GetQuestionInfofs(Question_Info, Pages.First, Pages.Max); ;
            QList.DataBind();
            return;
        }

        public string GetStatusName(object Statusid)
        {
            int t = Convert.ToInt32(Statusid);
            return Status.GetStatusInfoById(t).Name;
        }

        protected string GetPageFileName(int page)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("MyQuestion.aspx?");
            if (_QuesTypeId > 0)
            {
                sb.Append("QuesTypeId=");
                sb.Append(_QuesTypeId);
                sb.Append("&");
            }
            if (_SubjectId > 0)
            {
                sb.Append("SubjectId=");
                sb.Append(_SubjectId);
                sb.Append("&");
            }
            if (_Keyword != "")
            {
                sb.Append("Keyword=");
                sb.Append(_Keyword);
                sb.Append("&");
            }
            if (Question_Info.ComputeTime > 0)
            {
                sb.Append("ComputeTime=");
                sb.Append(Question_Info.ComputeTime);
                sb.Append("&");
            }
            if (Question_Info.CognitiveLevelId > 0)
            {
                sb.Append("CognitiveLevelId=");
                sb.Append(Question_Info.CognitiveLevelId);
                sb.Append("&");
            }
            if (Question_Info.DifficId > 0)
            {
                sb.Append("DifficId=");
                sb.Append(Question_Info.DifficId);
                sb.Append("&");
            }
            if (Question_Info.BookId > 0)
            {
                sb.Append("BookId=");
                sb.Append(Question_Info.BookId);
                sb.Append("&");
            }
            if (Question_Info.Score > 0)
            {
                if (Question_Info.Score > 0)
                {
                    sb.Append("Score=");
                    sb.Append(Question_Info.Score);
                    sb.Append("&");
                }
            }
            if (Question_Info.KnowId > 0)
            {
                if (Question_Info.KnowId > 0)
                {
                    sb.Append("KnowId=");
                    sb.Append(Question_Info.KnowId);
                    sb.Append("&");
                }
            }
            sb.Append("page=");
            if (page > 0)
            {
                sb.Append(page);
            }
            return sb.ToString();
        }

        protected void SetSubjectId(int inputSubjectId)
        {
            LoadKnowledge(0, inputSubjectId, "——");
            LoadBook(inputSubjectId);
            //KnowIdList.Text = "";
            BookList.Visible = true;
            KnowIdList.Visible = true;
        }

        protected void SubjectIdList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (SubjectList.SelectedIndex > -1)
            {
                int tSubjectId = int.Parse(SubjectList.SelectedValue.Trim());
                SetSubjectId(tSubjectId);
            }
        }

        protected void QList_ItemDataBound(object sender, ListViewItemEventArgs e)
        {

        }
        //加入试题的状态（当试题为暂存的状态时才显示试题状态为暂存）
        protected void lbStatus_DataBinding(object sender, EventArgs e)
        {
            Label lb = sender as Label;
            if (lb != null)
            {
                try
                {
                    if (lb.Text == "" || lb.Text == "0")
                    {
                        lb.Text = "";
                    }
                    else
                    {
                        //DifficInfo item = Diffic.GetDifficInfoById(int.Parse(lb.Text));
                        if (lb.Text.Trim()!=null && lb.Text.Trim()!="")
                        {
                            StatusInfo item = Status.GetStatusInfoById(int.Parse(lb.Text.Trim()));
                            if (item != null && item.Id != 0)
                            {
                                if (lb.Text.Trim() != "10")
                                {
                                    lb.Text = "";
                                }
                                else
                                {
                                    lb.Text = "【" + item.Name + "】";
                                }
                                
                            }
                            else 
                            {
                                lb.Text = "";
                            }
                        }
                        
                    }
                }
                catch
                {
                    lb.Text = "";
                }
            }
        }
    }
}
